local panyun = fk.CreateSkill{
  name = "hanqing__panyun",
}

Fk:loadTranslationTable{
  ["hanqing__panyun"] = "攀云",
  [":hanqing__panyun"] = "若你上轮未回复过体力，你的判定区视为拥有【平步青云】；判定阶段或弃牌阶段开始时，若你的判定区没有牌，"..
  "你令一名角色可选择一项：1.使用一张【杀】，获得〖攀云〗；2.视为使用一张【杀】，获得〖崩坏〗。",

  ["#hanqing__panyun-choose"] = "攀云：令一名角色选择使用【杀】或视为使用【杀】",
  ["hanqing__panyun1"] = "使用一张【杀】，获得“攀云”",
  ["hanqing__panyun2"] = "视为使用一张【杀】，获得“崩坏”",
  ["#hanqing__panyun1-use"] = "攀云：你可以使用一张【杀】，获得“攀云”",
  ["#hanqing__panyun2-use"] = "攀云：你可以视为使用一张【杀】，获得“崩坏”",
  ["@hanqing__panyun"] = "攀云",
  ["hanqing__panyun_banner1"] = "使用",
  ["hanqing__panyun_banner2"] = "视为使用",

  ["$hanqing__panyun1"] = "蒙国君恩遇，得使冉平步青云。",
  ["$hanqing__panyun2"] = "豢鹰犬震声国内，挥王师开疆域外！",
  ["$hanqing__panyun3"] = "越二国而下齐，以为子孙后计。",
}

panyun:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(panyun.name) and
      (player.phase == Player.Judge or player.phase == Player.Discard) then
      local room = player.room
      local round_event = room.logic:getCurrentEvent():findParent(GameEvent.Round)
      if round_event == nil then
        if player.phase == Player.Judge then
          event:setCostData(self, {choice = 1})
          return true
        end
        return
      end
      local start_id, end_id = 0, 0
      room.logic:getEventsByRule(GameEvent.Round, 1, function (e)
        if e.id ~= round_event.id then
          start_id, end_id = e.id, e.end_id
          return true
        end
      end, 0)
      if start_id == 0 then
        if player.phase == Player.Judge then
          event:setCostData(self, {choice = 1})
          return true
        end
        return
      end
      if #room.logic:getEventsByRule(GameEvent.Recover, 1, function (e)
        return e.id <= end_id and e.data.who == player
      end, start_id) == 0 then
        if player.phase == Player.Judge then
          event:setCostData(self, {choice = 1})
          return true
        end
        return
      elseif #player:getCardIds("j") == 0 and not table.contains(player.sealedSlots, Player.JudgeSlot) then
        event:setCostData(self, {choice = 2})
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(panyun.name)
    local choice = event:getCostData(self).choice
    if choice == 1 then
      room:notifySkillInvoked(player, panyun.name, "support")
      local card = Fk:cloneCard("hanqing__meteoric_rise")
      card.skillName = panyun.name
      card.skill:onEffect(room, {
        to = player,
        card = card,
      })
    elseif choice == 2 then
      room:notifySkillInvoked(player, panyun.name, "control")
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = room.alive_players,
        skill_name = panyun.name,
        prompt = "#hanqing__panyun-choose",
        cancelable = false,
      })[1]
      choice = room:askToChoice(to, {
        choices = {"hanqing__panyun1", "hanqing__panyun2", "Cancel"},
        skill_name = panyun.name,
      })
      if choice == "hanqing__panyun1" then
        local use = room:askToUseCard(to, {
          skill_name = panyun.name,
          pattern = "slash",
          prompt = "#hanqing__panyun1-use",
          extra_data = {
            bypass_times = true,
          },
          cancelable = true,
        })
        if use then
          choice = "hanqing__panyun_banner1"
          use.extraUse = true
          room:useCard(use)
          if not to.dead then
            room:handleAddLoseSkills(to, panyun.name)
          end
        else
          return
        end
      elseif choice == "hanqing__panyun2" then
        local use = room:askToUseVirtualCard(to, {
          name = "slash",
          skill_name = panyun.name,
          prompt = "#hanqing__panyun2-use",
          cancelable = true,
          extra_data = {
            bypass_times = true,
            extraUse = true,
          },
          skip = true,
        })
        if use then
          choice = "hanqing__panyun_banner2"
          use.extraUse = true
          room:useCard(use)
          if not to.dead then
            room:handleAddLoseSkills(to, "hanqing__benghuai")
          end
        else
          return
        end
      else
        return
      end
      local banner = room:getBanner("@hanqing__panyun")
      room:setBanner("@hanqing__panyun", choice)
      if banner == choice and
        table.find(room.alive_players, function (p)
          return p:hasSkill("hanqing__yitu")
        end) then
        room:setBanner("hanqing__yitu", panyun.name)
      end
    end
  end,
})

return panyun
